home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 11.7 KB | 317 lines | [TEXT/MPS ] |
- // UClassDesc.h
- // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
-
-
- #ifndef __UCLASSDESC__
- #define __UCLASSDESC__
-
- // MacApp
-
- #ifndef __MACAPPTYPES__
- #include "MacAppTypes.h"
- #endif
-
- #ifndef __PASCALSTRING__
- #include "PascalString.h"
- #endif
-
- // Toolbox
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- // ANSI
-
- #ifndef __STDDEF__
- #include <stddef.h>
- #endif
-
- //----------------------------------------------------------------------------------------
- // Forward class declarations
- //----------------------------------------------------------------------------------------
-
- class TClassDescListByName;
- class TClassDescListByID;
- class TClassDescSignatureList;
- class ClassDesc;
-
- #ifndef qMultipleInheritance
- #define qMultipleInheritance TRUE
- #endif
-
- //----------------------------------------------------------------------------------------
- // ClassID: A short, to minimize the amount of global space used by the type information.
- // ClassName: Returned by ClassDesc::GetClassName(). Internally we store class name as a
- // c-string. It saves space.
- //----------------------------------------------------------------------------------------
-
- typedef unsigned short ClassID;
- typedef CStr255 ClassName;
- typedef long ArrayIndex;
-
-
- //----------------------------------------------------------------------------------------
- // Some constants
- //----------------------------------------------------------------------------------------
-
- const ClassID kNilClass = 0;
-
-
- //----------------------------------------------------------------------------------------
- // CLASS ClassDesc
- //----------------------------------------------------------------------------------------
-
- typedef void * (* ConstructorProcPtr)();
-
- class CClassIterator;
-
- class ClassDesc;
- typedef const ClassDesc* MA_ClassReference;
-
- #if qTheDebugger && PRAGMA_ALIGN_SUPPORTED
- #pragma options align=mac68k // TheDebugger looks at these
- #endif
-
- class ClassDesc
- {
- friend CClassIterator; // All Class iterator
-
- public:
- ClassDesc(const char* className, size_t classSize,
- MA_ClassReference const *ancestors,
- const ptrdiff_t *const offsets);
-
- void* Call_DefaultConstructor() const;
- // Return a new instance of the class.
-
- ArrayIndex CountBaseClasses() const;
- // Return a count of the number of ancestors.
-
- void* DynamicCast(const void* p, MA_ClassReference pointersClass,
- MA_ClassReference aBaseClass) const;
- // Returns nonzero pointer if p!=0 and DescendsFrom(aBaseClass), otherwise return 0.
-
- inline void GetClassName(ClassName& clName) const
- { clName = fClassName; }
- // Return a copy of the class name.
-
- inline const char* GetClassName() const
- { return fClassName; }
- // Return a pointer to the class name.
-
- inline ClassID GetClassID() const
- { return fClassID; }
- // Return the class ID.
-
- inline void SetClassID(ClassID theClassID)
- { fClassID = theClassID; }
- // Allow setting the class ID (used by Jasik's debugger).
-
- inline const size_t GetClassSize() const
- { return fClassSize; }
- // Returns the size of instances of the class.
-
- MA_ClassReference GetBaseClass(ArrayIndex index = 1) const;
- // Return a reference to the class info for an ancestor class.
-
- inline MA_ClassReference GetNextClassDesc() const
- { return fNextClassDesc; }
- // Return a reference to the next class info record (not in any
- // particular order).
-
- IDType GetSignature() const;
- // Looks up the signature used for creating this class.
-
- Boolean DescendsFrom(MA_ClassReference theClass, Boolean directDescendant = FALSE) const;
- // Return true if the class is or inherits from class represented by theClass.
- // Note, usage is: this->DescendsFrom(theClass). See also MA_MEMBER.
-
- inline void RegisterClass(ConstructorProcPtr constructor)
- { fDefaultConstructor = constructor; }
- // Register a function that creates an instance of this class.
- // See MA_REGISTER_CLASS.
-
- void RegisterSignature(IDType signature);
- // Specify a signature for efficiently creating instances of this class
- // from streams. See MA_REGISTER_SIGNATURE.
-
- private:
- Boolean OffsetToBase(MA_ClassReference aBaseClass, ptrdiff_t &offset) const;
- //
-
- //----------------------------------------------------------------------------------------
- // static member functions
- //----------------------------------------------------------------------------------------
- public:
- static void InitUClassDesc();
-
- static Boolean IsValidClassID(ClassID classID);
-
- static void* NewByClassID(ClassID classID);
- static void* NewByClassName(const ClassName& className);
- static void* NewBySignature(IDType signature);
-
- static MA_ClassReference GetClassDescFromClassID(ClassID classID);
- static MA_ClassReference GetClassDescFromClassName(const ClassName& className);
- static MA_ClassReference GetClassDescFromSignature(IDType signature);
-
- inline static void CallRegisterClass(MA_ClassReference classDesc, ConstructorProcPtr constructor)
- { ((ClassDesc*) classDesc)->RegisterClass(constructor); }
-
- inline static void CallRegisterSignature(MA_ClassReference classDesc, IDType signature)
- { ((ClassDesc*) classDesc)->RegisterSignature(signature); }
-
- //----------------------------------------------------------------------------------------
- // data members
- //----------------------------------------------------------------------------------------
- private:
- const char* fClassName; // Class name
- size_t fClassSize; // Class size
- MA_ClassReference const *fAncestors; // ClassInfo for ancestors of the class.
- // Pointer to array of const pointers to ClassDesc
- const ptrdiff_t *const fAncestorOffsets; // Object pointer offsets for ancestors of the class.
-
- MA_ClassReference fNextClassDesc; // Link to next ClassDesc in list of all ClassDesc.
- ClassID fClassID; // Unique identifying class ID.
-
- ConstructorProcPtr fDefaultConstructor; // Default constructor for this class
-
- //----------------------------------------------------------------------------------------
- // static data members
- //----------------------------------------------------------------------------------------
- public:
- // fgClassDescList needs to be public to satisfy a bug with Symantec's SCpp where
- // it fails to correctly deal with friend classes…
- static ClassDesc* fgClassDescList; // Head of linked list of
- // ClassDesc, created at static
- // initialization time.
-
- private:
- static TClassDescListByName* fgClassDescListByName; // List of ClassDesc by class name.
- static TClassDescListByID* fgClassDescListByID; // List of ClassDesc, indexed by class ID.
- static TClassDescSignatureList* fgSignatures; // Standard object signatures and their classIDs
- static ClassID fgNextClassID; // The next unique class ID.
-
- };
-
- #if qTheDebugger && PRAGMA_ALIGN_SUPPORTED
- #pragma options align=reset
- #endif
-
- //========================================================================================
- // Macros for generating and accessing class information
- //========================================================================================
-
- #ifndef __UCLASSDESC_PRIVATE__
- #include "UClassDesc.Private.h"
- #endif
-
- // DeclareClassDesc macro: Left for compatibility. Does forward reference of
- // class, which can't hurt.
-
- #define DeclareClassDesc(name) \
- class name
-
- //----------------------------------------------------------------------------------------
- // DeclareClass macro: Declares methods and fields of application class <name> necessary
- // to access type information for the class. The DeclareClass should
- // be used once for each application class declaration immediately
- // following the '{' for the application class <name>.
- //
- // class TCommand : public TEvent
- // {
- // MA_DECLARE_CLASS;
- //
- // public:
- // TCommand();
- //----------------------------------------------------------------------------------------
-
- #define DeclareClass(name) MA_DECLARE_CLASS
-
- #define MA_DECLARE_CLASS \
- public: \
- static const ClassDesc fgClassDesc; \
- static const MA_ClassReference fgAncestors[]; \
- static const ptrdiff_t fgAncestorOffsets[]; \
- virtual MA_ClassReference GetClassDescDynamic() const; \
- inline static MA_ClassReference GetClassDescStatic() { return &fgClassDesc; } \
- static void *_DefaultConstructor()
-
- //----------------------------------------------------------------------------------------
- // DefineClass macro: Defines data and methods necessary for class <name> to access and
- // implement type information for the class. Should be used once in the
- // implementation file for <name>.
- //
- // #undef Inherited
- // #define Inherited TEvent
- // #pragma segment MACommandNonRes
- // MA_DEFINE_CLASS_M1(TCommand, Inherited);
- //----------------------------------------------------------------------------------------
-
- #define MA_DEFINE_CLASS_M0(name) \
- _MA_CLASS_ANCESTORS_IMPLEMENT_M0(name) \
- _MA_DEFINE_CLASSINFO_OBJECT(name) \
- _MA_DEFINE_CLASS_METHODS(name)
-
- #define MA_DEFINE_CLASS_M1(name, ancestor) \
- _MA_CLASS_ANCESTORS_IMPLEMENT_M1(name, ancestor) \
- _MA_DEFINE_CLASSINFO_OBJECT(name) \
- _MA_DEFINE_CLASS_METHODS(name)
-
- #define MA_DEFINE_CLASS_M2(name, ancestor1, ancestor2) \
- _MA_CLASS_ANCESTORS_IMPLEMENT_M2(name, ancestor1, ancestor2) \
- _MA_DEFINE_CLASSINFO_OBJECT(name) \
- _MA_DEFINE_CLASS_METHODS(name)
-
- #define MA_DEFINE_CLASS_M3(name, ancestor1, ancestor2, ancestor3) \
- _MA_CLASS_ANCESTORS_IMPLEMENT_M3(name, ancestor1, ancestor2, ancestor3) \
- _MA_DEFINE_CLASSINFO_OBJECT(name) \
- _MA_DEFINE_CLASS_METHODS(name)
-
- #define MA_DEFINE_CLASS_M4(name, ancestor1, ancestor2, ancestor3, ancestor4) \
- _MA_CLASS_ANCESTORS_IMPLEMENT_M4(name, ancestor1, ancestor2, ancestor3, ancestor4) \
- _MA_DEFINE_CLASSINFO_OBJECT(name) \
- _MA_DEFINE_CLASS_METHODS(name)
-
- #define DefineClass(name, firstBase) \
- MA_DEFINE_CLASS_M1(name, firstBase)
-
- //----------------------------------------------------------------------------------------
- // Class Registry Macros: Forces the class <name> to be marked active so that the
- // linker does not dead strip it. Makes it possible for the
- // class to be created by name or by signature.
- //----------------------------------------------------------------------------------------
-
- #define MA_REGISTER_CLASS(name) \
- ClassDesc::CallRegisterClass(&name::fgClassDesc, &name::_DefaultConstructor);
-
- #define MA_REGISTER_SIGNATURE(name, signature) \
- ClassDesc::CallRegisterClass(&name::fgClassDesc, &name::_DefaultConstructor); \
- ClassDesc::CallRegisterSignature(&name::fgClassDesc, signature)
-
- // macroDontDeadStrip macro: Left for compatibility.
-
- #define macroDontDeadStrip(name) \
- MA_REGISTER_CLASS(name)
-
- //----------------------------------------------------------------------------------------
- // Object Member Macro (simulates the pascal Member function).
- //----------------------------------------------------------------------------------------
-
- #define MA_MEMBER(object, class) \
- (((object)->GetClassDescDynamic())->DescendsFrom(&class::fgClassDesc))
-
- //----------------------------------------------------------------------------------------
- // Dynamic Cast Macro
- //----------------------------------------------------------------------------------------
-
- // The dynamic cast macro checks the object's run time type information to see if it
- // actually descends from the class you want to cast it to. If it does, it performs
- // the cast, otherwise it returns NULL.
-
- #define MA_DYNAMIC_CAST(T, p) \
- ( (p) ? (T*) (((p)->GetClassDescDynamic())->DynamicCast(p, &(p)->fgClassDesc, &T::fgClassDesc)) : (T*) 0)
-
- #endif // __UCLASSDESC__
-